﻿using System;

namespace JetBlack.FeedBus.ExcelAddin
{
    internal class FeedTopicFieldKey : IEquatable<FeedTopicFieldKey>, IComparable<FeedTopicFieldKey>
    {
        internal readonly string Feed;
        internal readonly string Topic;
        internal readonly string Field;

        internal FeedTopicFieldKey(string feed, string topic, string field)
        {
            Feed = feed;
            Topic = topic;
            Field = field;
        }

        public override bool Equals(object obj)
        {
            return Equals(obj as FeedTopicFieldKey);
        }

        public override int GetHashCode()
        {
            return
                (Feed == null ? 0 : Feed.GetHashCode())
                ^ (Topic == null ? 0 : Topic.GetHashCode())
                ^ (Field == null ? 0 : Field.GetHashCode());
        }

        public override string ToString()
        {
            return string.Concat(Feed, ":", Topic, ":", Field);
        }

        public bool Equals(FeedTopicFieldKey other)
        {
            return
                other != null
                && String.Equals(Feed, other.Feed)
                && String.Equals(Topic, other.Topic)
                && String.Equals(Field, other.Field);
        }

        public int CompareTo(FeedTopicFieldKey other)
        {
            if (other == null)
                return 1;

            if (Feed == null && other.Feed != null)
                return -1;
            if (Topic == null && other.Topic != null)
                return -1;

            int i = Feed.CompareTo(other.Feed);
            if (i == 0)
                i = Topic.CompareTo(other.Topic);
            if (i == 0)
                i = Field.CompareTo(other.Field);
            return i;
        }
    }
}
